' help.bmx

Strict

Import brl.filesystem

Import "host.bmx"
Import "nodeview.bmx"
Import "opencode.bmx"

Type THelpPanel Extends TToolPanel

	Field	host:THost
	Field	htmlview:TGadget
	Field	address$
	Field	findscript$
	Field	findarg$

	Method Invoke(command$,argument:Object=Null)
		Local	href$
		If Not htmlview Return
		Select command
			Case "CUT"
				GadgetCut htmlview
			Case "COPY"
				GadgetCopy htmlview
			Case "PASTE"
				GadgetPaste htmlview
			Case "SHOW"
				ActivateGadget htmlview	
				htmlview.run "window.focus()"
				host.SetTitle GadgetText(htmlview)'address
			Case "FIND"
				host.ShowFind findarg$
			Case "FINDNEXT"
				Local srch$,script$				
				srch=String(argument)
				If Not srch srch=findarg
				If srch
					findarg=srch
					htmlview.run "findnext(~q"+srch+"~q);"
				EndIf
			Case "NAVIGATE"
				href$=String(argument)
				If href Go href
			Case "PRINT"
				GadgetPrint htmlview
		End Select
	End Method

	Method OnEvent()
		Local	url$,p,t$
		If EventSource()=htmlview
			Select EventID()			
				Case EVENT_GADGETACTION
					url$=String( EventExtra() )
					If url[..5]="http:" Or url[..6]="https:" 
						OpenURL url
					Else
						p=url.findlast(".")
						If p>-1
							t$=url[p..].tolower()
							If t$=".bmx"
								If url.Find( "file://" )=0
									url=url[7..]
									?Win32
									url=url[1..]
									?
								EndIf 
								url=url.Replace("%20"," ")
								Local source:TOpenCode=TOpenCode(host.OpenSource(url))
								If source source.MakePathTemp
							Else 
								url=url.Replace("\","/")
								url=url.Replace("user/index","user/welcome")
								url=url.Replace("lang/index","lang/welcome")
								url=url.Replace("mods/index","mods/welcome")
								Go url$
							EndIf
						EndIf
					EndIf
					
				Case EVENT_GADGETDONE
					HtmlViewRun htmlview,findscript
					
			End Select			
		EndIf
	End Method
	
	Method Go(url$)
		Local	node:TDataNode

		HtmlViewGo htmlview,url$
		host.SelectPanel Self

		node=host.docroot.FindArgument(RealPath(url))
		If node
			node.Highlight
		Else
'			print "node not found"
		EndIf		

		ActivateGadget htmlview			
		htmlview.run "window.focus()"
		address=url
	End Method

	Method CompileHelp$()		
		Local plugin:TPlugin	
		Local h$,html$,url$
		html$="<html><head><title>MaxEdit Help</title></head><body>"	'<h1>MaxEdit Home</h1>
		html:+"<p>Generated on "+CurrentDate()+"<p>"
		For plugin=EachIn host.pluginlist
			h$=plugin.GetStringProperty("Help")
			If h$ html$:+h$
		Next	
		html:+"</body></html>~n~n"		
		url$=host.CreateTempFile$("html")
		SaveText html$,url
		Return url$
	End Method
	
	Method Home()
		Local url$
		Go "about:blank"	'whatthe? cludge for OSX to avoid blank page on startup
		url=CompileHelp()
		Go url
	End Method

	Method Index()
'		Go AppDir+"/index.html"
	End Method
	
	Method Forward()
		HtmlViewForward htmlview
	End Method

	Method Back()
		HtmlViewBack htmlview
	End Method

	Function GetFindScript$()
		Local f$
		f$="var tr;"
		f:+"function findnext(f){"
		f:+"if (!tr) tr=document.body.createTextRange();"
		f:+"if (tr.findText(f)==true) {tr.select();tr.collapse(false);} else tr=null;"
		f:+"}"
		Return f
	End Function

	Function Create:THelpPanel(owner:THost)
		Local	p:THelpPanel
		Local	root,style		
		p=New THelpPanel
		p.host=owner
		p.name="Help"
		p.active=True
		p.findscript=getfindscript()
		owner.addpanel(p)
		style=HTMLVIEW_NONAVIGATE		'HTMLVIEW_NOCONTEXTMENU
		p.htmlview=CreateHTMLView(0,0,ClientWidth(p.panel),ClientHeight(p.panel),p.panel,style)		
		SetGadgetLayout p.htmlview,1,1,1,1
		p.Home
'		p.buildtree
		Return p
	End Function

End Type



Rem
						
				Case EVENT_GADGETDONE
'					HtmlViewRun htmlview,"window.focus()"
'					url$=EventText()
'					Print "gadgetdone:"+htmlview.gettext()	'gadgettext(url

					url$=EventText()
					p=url.findlast(".")
					If p>-1
					
						t$=url[p..].tolower()
						If t$=".bmx" 
							host.OpenSource url
						Else 
							url=url.Replace("\","/")
							url=url.Replace("user/index","user/welcome")
							url=url.Replace("lang/index","lang/welcome")
							url=url.Replace("mods/index","mods/welcome")
							Go url$
						EndIf
					EndIf	
				Default
					Print "help panel eventid="+EventID()
EndRem				
Rem
		Local	node:TNode
		node=host.helproot.FindArgument(RealPath(url))
		If node
			node.Highlight
		Else
'			print "node not found"
		EndIf		
EndRem		
